/*
题目链接 : https://leetcode.cn/problems/most-beautiful-item-for-each-query/?envType=daily-question&envId=2025-03-09
*/

//题解代码 : 
class Solution {
public:
    vector<int> maximumBeauty(vector<vector<int>>& items, vector<int>& queries) {
        sort(items.begin(),items.end(),[&](const vector<int>& p1,const vector<int>& p2)->bool{
            return p1[0] < p2[0];
        });
        int n = items.size();
        for(int i=1;i<n;++i){
            items[i][1] = max(items[i-1][1],items[i][1]);
        }
        int m = queries.size();
        vector<int> ans(m);
        for(int i=0;i<m;++i){
            int l = 0,r = n-1;
            while(l<=r){
                int mid = (l+r)/2;
                if(items[mid][0]<=queries[i]) l = mid+1;
                else r = mid-1;
            }
            if(r>=0)ans[i] = items[r][1];
        }
        return ans;
    }
};
